<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    width="50%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px' }"
    @close="onClose"
  >
    <a-tabs v-model="tabIndex">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文"></a-tab-pane>
    </a-tabs>
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      :key="visible"
    >
      <a-form-model-item label="类型" prop="type">
        <a-select v-model="form.type" placeholder="类型">
          <a-select-option :value="1">视频</a-select-option>
          <a-select-option :value="2">情怀图片</a-select-option>
          <a-select-option :value="3">公司理念</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="标题" prop="title" v-if="tabIndex == 1">
        <a-input v-model="form.title" placeholder="标题"></a-input>
      </a-form-model-item>
      <a-form-model-item label="英文标题" prop="titleEn" v-else>
        <a-input v-model="form.titleEn" placeholder="英文标题"></a-input>
      </a-form-model-item>
      <a-form-model-item label="描述" v-if="tabIndex == 1">
        <a-textarea
          v-model="form.description"
          placeholder="描述"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>
      <a-form-model-item label="英文视频描述" v-else>
        <a-textarea
          v-model="form.descriptionEn"
          placeholder="菜单描述"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>
      <a-form-model-item :label="form.type==1?'视频封面':'图片'" required>
        <a-upload
          list-type="picture-card"
          :file-list="fileList"
          :remove="hanldeImgRemove"
          @preview="handlePreview"
          :customRequest="handleUploadChange"
          class="upload-half"
          accept="image/*"
        >
          <div class="custom-btn" v-if="fileList.length < 1">
            <a-icon type="ant-cloud" style="font-size: 26px" />
            <div class="ant-upload-text">上传图片</div>
          </div>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item label="视频" required v-if="form.type==1">
        <a-upload
          list-type="picture-card"
          :file-list="fileList1"
          :remove="hanldeImgRemove1"
          @preview="handlePreview"
          :customRequest="handleUploadChange1"
          class="upload-half"
          accept="video/*"
        >
          <div class="custom-btn" v-if="fileList1.length < 1">
            <a-icon type="ant-cloud" style="font-size: 26px" />
            <div class="ant-upload-text">上传视频</div>
          </div>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item label="排序">
        <a-input-number v-model="form.sortNum" placeholder="排序"/>
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible = false" width="60%">
      <img alt="example" style="width: 100%" :src="previewImage" v-if="previewImage.indexOf('.mp4') == -1" />
      <video alt="example" style="width: 100%" :src="previewImage" v-else controls />
    </a-modal>
  </a-drawer>
</template>

<script>
import { insertVideoList, updateVideoList } from '@/api/manage'
import { showMessage } from '@/utils/mixins'
import { uploadFile,deleteFile } from '@/api/common'
import { getFileName } from '@/utils/util'

export default {
  mixins: [showMessage],
  props: {
    parent: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      tabIndex: '1',
      visible: false,
      title: '',
      labelCol: { span: 3 },
      wrapperCol: { span: 21 },
      form: {
        type: undefined,
        posterUrl: undefined,
        videoUrl: undefined,
        title: undefined,
        titleEn: undefined,
        description: undefined,
        descriptionEn: undefined,
        sortNum: 0,
      },
      rules: {
        type: [{ required: true, message: '年份不能为空', trigger: 'change' }],
        year: [{ required: true, message: '年份不能为空', trigger: 'change' }],
        month: [{ required: true, message: '月份不能为空', trigger: 'change' }],
        title: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
      },
      status: undefined,
      id: undefined,
      fileList: [],
      fileList1: [],
      previewVisible: false,
      previewImage: '',
    }
  },

  methods: {
    // 预览
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    //删除视频
    hanldeImgRemove1(file) {
      deleteFile({ id: file.id }).then((res) => {
        if (res.code == 200) {
          this.showMessage(res, () => {
            this.form.videoUrl = undefined
            this.fileList1 = []
          })
        }
      })
    },
    //删除图片
    hanldeImgRemove(file) {
      deleteFile({ id: file.id }).then((res) => {
        if (res.code == 200) {
          this.showMessage(res, () => {
            this.form.posterUrl = undefined
            this.fileList = []
          })
        }
      })
    },
    // 上传视频
    handleUploadChange1(file) {
      const _this = this
      const formdata = new FormData()
      formdata.append('file', file.file)
      uploadFile(formdata).then((res) => {
        if (res.code == 200) {
          file.url = res.data.link
          file.uid = res.data._id
          file.id = res.data._id
          file.name = getFileName(res.data.link)
          _this.fileList1.push(file)
          _this.form.videoUrl = res.data._id
        } else {
          _this.$message.error(res.message)
        }
        this.cropperVisible = false
      })
    },
    //自定义上传图片
    handleUploadChange(file) {
      const _this = this
      const formdata = new FormData()
      formdata.append('file', file.file)
      uploadFile(formdata).then((res) => {
        if (res.code == 200) {
          file.url = res.data.link
          file.uid = res.data._id
          file.id = res.data._id
          file.name = getFileName(res.data.link)
          _this.fileList.push(file)
          _this.form.posterUrl = res.data._id
        } else {
          _this.$message.error(res.message)
        }
        this.cropperVisible = false
      })
    },
    // 提交
    onSubmit() {
      this.$refs.ruleForm.validate((vaild) => {
        if (vaild) {
          if (this.status == 1) {
            insertVideoList(this.form).then((res) => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          } else {
            updateVideoList({
              id: this.id,
              ...this.form,
            }).then((res) => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          }
        } else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      for (const key in this.form) {
        if (key == 'sortNum') {
          this.form[key] = 0
        } else {
          this.form[key] = undefined
        }
      }
      this.status = undefined
      this.visible = false
      this.id = undefined
      this.fileList = []
      this.fileList1 = []
    },
  },
}
</script>